{% extends 'app01/moban/bassic.html' %}

{% block title %}用 户 注 册{% endblock %}

{% block css %}
    <style>
        .top1 {
            padding: 20px 20px 20px 20px;
            width: 450px;
            margin: 20px auto 0;
            border: #bcd0c7 solid 1px;
            border-radius: 20px 20px 20px 20px;
            box-shadow: 4px 4px 15px 1px #adb5bd;
        }

        .text {
            font-weight: bold;
        }

        input::placeholder {
            font-size: 13px;
        }

        .error_span {
            color: #b92c28;
            position: absolute;
            font-size: 12px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="top1">
        <h3 class="text" style="text-align: center">用户注册</h3>
        <form id='regForm' method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == "code" %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}" class="form-label text">{{ field.label }}</label>
                        <div class="clearfix">
                            <div class="col-md-7" style="padding-left: 0">
                                {{ field }}
                                <span class="error_span"></span>
                            </div>
                            <div class="col-md-5" style="padding-left:  28px">
                                <button id='btnsms' type="button" class="btn btn-default">点击获取验证码</button>
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}" class="form-label text">{{ field.label }}</label>
                        {{ field }}
                        <span class="error_span"></span>
                    </div>
                {% endif %}
            {% endfor %}
            <div class="d-grid gap-2 col-6 mx-auto">
                <p>
                    <button id='btnSubmit' type="button"
                            style="height: 40px; width: 30%;line-height: 20px;margin: 20px auto 0;"
                            class="btn btn-primary btn-lg btn-block">注 册
                    </button>
                </p>
            </div>
        </form>

    </div>
{% endblock %}
{% block js %}
    <script>
        /**页面加载默认执行函数*/
        $(function () {
            bingsms()
            bingSubmit()
        });

        /** 点击提交*/
        function bingSubmit() {
            $('#btnSubmit').click(function () {
                $('.error_span').empty()
                $.ajax({
                    url: "",
                    type: 'POST',
                    data: $('#regForm').serialize(), // form所有字段加上csrf token //收集表单里所有的数据
                    success: function (res) {
                        if (res.status) {
                            location.href = res.data
                        } else {
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            })
                        }
                    }
                })
            })
        }

        /**验证码按钮点击事件*/
        function bingsms() {
            $('#btnsms').click(function () {
                $('.error_span').empty() /*点击前先清空错误信息*/
                var mobelPhone = $('#id_mobile_phone').val();
                /* 发送ajax请求 */
                $.ajax({
                    url: "/app01/sendsms/", /*/sendsms/*/
                    type: 'GET',
                    data: {mobile_phone: mobelPhone, tpl: 'register'},
                    dataType: "JSON", /*将服务器端返回的的数据反序列化为字典*/
                    success: function (res) {
                        /*发送成功之后的回调函数  res就是后端返回的值*/
                        if (res.status) {
                            sendSmsRemind();
                            console.log('发送成功')
                        } else {
                            console.log('发送失败', res)
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            })
                        }
                    }
                })
            })
        }

        /**倒计时功能*/
        function sendSmsRemind() {
            var smsBtn = $('#btnsms');
            smsBtn.prop('disabled', true); /*禁用*/
            var time = 60
            var remind = setInterval(function () {
                smsBtn.text(time + '秒重新发送');
                time = time - 1;
                if (time < 1) {
                    clearInterval(remind);
                    smsBtn.text('点击获取验证码').prop('disabled', false);
                }
            }, 1000)
        }
    </script>
{% endblock %}